package com.rmdjzz.slmmn.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @作者 张绍鑫
 * @创建时间 2023/6/11
 */
@Component
@Slf4j
public class AdminInterceptor implements HandlerInterceptor {

    /**
     * 目标方法执行前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Object loginUser  = request.getSession().getAttribute("admin");
        System.out.println(1111);
        System.out.println(request.getMethod());
        if (loginUser == null) {
            //未登录，返回登陆页
            log.info("拦截管理员");
            request.getRequestDispatcher("/admin/adminLogin").forward(request, response);
            return false;
        } else {
            //放行
            return true;
        }

    }
}
